<?php
class ss_doctrine_jsonrefhydrator extends Doctrine_Hydrator_RecordDriver
{
	static public $JSON_HYDRATOR_PREFIX;
	protected $fold;
	protected $mainRun=true;
	
    public function getElementCollection($component)
    {
        return array();
    }

    public function getElement(array $data, $_class)
    {
    	//$_class=$component;
        if ( ! isset($this->_tables[$_class])) {
            $this->_tables[$_class] = Doctrine::getTable($_class);
        	$this->_tables[$_class]->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, false);
        }
        if(!is_array($this->fold)) {
        	$this->fold=$this->_tables[$_class]->getFold();
			foreach($this->fold['class'] as $_fclass=>$i){
	            $this->_tables[$_fclass] = Doctrine::getTable($_fclass);
	            $this->_tables[$_fclass]->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, false);
			}
		}
		if(@$this->fold['class'][$_class]) {
			$_id=$this->_tables[$_class]->getIdentifier();
			if(is_array($_id)) {
				$_ref=array();
				foreach($_id as $_key){
					$_ref[]=$data[$_key];
				}
				$_ref=implode('_',$_ref);
			} else {
				$_ref=$data[$_id];
			}
				if(count($data)==1) $data=array('$ref'=>self::$JSON_HYDRATOR_PREFIX.$_class.'/'.$_ref);
		} elseif(strpos($_class,'Translation')!==false) {
			unset($data['id']);
		}
		foreach($this->fold['field'] as $fname=>$_fold){
			if(array_key_exists($fname,$data)) {
				if(is_null($data[$fname])){
					$data[$_fold['alias']]=null;
				} else {
					$data[$_fold['alias']]=array('$ref'=>self::$JSON_HYDRATOR_PREFIX.$_fold['class'].'/'.$data[$fname]);
				}
				unset($data[$fname]);
			}
		}
        $this->_tables[$_class]->setData($data);
		if($_class===$this->fold['NestedSet']) {
        	$record = $this->_tables[$_class]->getRecord();
			$data['children']=$record->getChildren();
		}
        return $data;
    }

    public function registerCollection($coll)
    {
    }

    public function initRelated(&$record, $name)
    {
        if ( ! isset($record[$name])) {
            $record[$name] = array();
        }
        return true;
    }

    public function getNullPointer() 
    {
        return null;    
    }

    public function getLastKey(&$coll)
    {
        end($coll);
        return key($coll);
    }

    public function setLastElement(&$prev, &$coll, $index, $dqlAlias, $oneToOne)
    {
        if ($coll === null) {
            unset($prev[$dqlAlias]); // Ticket #1228
            return;
        }

        if ($index !== false) {
            // Link element at $index to previous element for the component
            // identified by the DQL alias $alias
            $prev[$dqlAlias] =& $coll[$index];
            return;
        }
        
        if ($coll) {
            if ($oneToOne) {
            	//ss_error::log($index,'dqlAlias '.$dqlAlias);
                $prev[$dqlAlias] =& $coll;
            } else {
                end($coll);
                $prev[$dqlAlias] =& $coll[key($coll)];
            }
        }
    }
}
?>